package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.entity.Aircraft;
import org.example.entity.Airline;
import org.example.entity.Area;
import org.example.entity.Time;
import org.example.exception.MyException;
import org.example.mapper.AirlineMapper;
import org.example.service.AircraftService;
import org.example.service.AirlineService;
import org.example.service.AreaService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;

import java.time.LocalDate;
import java.util.*;

@RestController
@Api(tags = "航班管理")
@RequestMapping("/ml")
@CrossOrigin
public class AircraftController {
    @Autowired
    private AircraftService aircraftService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private AirlineService airlineService;

    @ApiOperation("获取特惠机票")
    @GetMapping("/get-gratia")
    public Result getGratia(){
        List<Aircraft> list=aircraftService.getGratia();
        return new Result<>("200","成功",list);
    }

    @ApiOperation("获取机票价格集合")
    @GetMapping("get-priceList")
    public Result getPriceList(String areaName1,String areaName2){
        Integer[] origin=aircraftService.getAreaId(areaName1,areaName2);
        List<Aircraft> list=aircraftService.list(new QueryWrapper<Aircraft>().eq("origin",origin[0]).eq("destination",origin[1]).isNull("airline_company"));
        List<Object> list2=new ArrayList<>();
        for (Aircraft aircraft : list) {
            Object[] arr={"'"+aircraft.getDate()+"'",aircraft.getPrice()};
            list2.add(arr);
        }
        return new Result<>("200","查询成功",list2);
    }

    @ApiOperation("获取当天机票最低价格")
    @GetMapping("get-min-price")
    public Result getMinPrice(String areaName1,String areaName2){
        List<Object> list=new ArrayList<>();
        try{
            Integer area1=areaService.getOne(new QueryWrapper<Area>().eq("name",areaName1)).getId();
            Integer area2=areaService.getOne(new QueryWrapper<Area>().eq("name",areaName2)).getId();
            String[] day={"周六","周日","周一","周二","周三","周四"};
            String[] days={"2025-04-06","2025-04-07","2025-04-08","2025-04-09","2025-04-10","2025-04-11"};
            for (int i = 0; i < days.length; i++) {
                Aircraft aircraft=new Aircraft();
                aircraft.setPrice(aircraftService.getMinPrice(days[i],area1,area2));
                aircraft.setDate(LocalDate.parse(days[i]));
                aircraft.setWeek(day[i]);
                list.add(JSONObject.toJSON(aircraft));
            }
        }catch (NullPointerException e){
            e.printStackTrace();
            throw new MyException("444","没有该城市");
        }
        return new Result<>("200","查询成功",list);
    }

    @ApiOperation("获取机票列表")
    @GetMapping("get-aircraft-list")
    public Result getAircraftList(String date,String area1,String area2,Boolean company1,Boolean company2,Boolean company3 ,Boolean time1,Boolean time2,Boolean time3,Boolean time4){
        List<Time> timeList=new ArrayList<>();
        List<Integer> companyList=new ArrayList<>();
        if (time1){
            timeList.add(new Time("00:00:00","05:59:59"));
        }
        if (time2){
            timeList.add(new Time("06:00:00","11:59:59"));
        }
        if (time3){
            timeList.add(new Time("12:00:00","17:59:59"));
        }
        if (time4){
            timeList.add(new Time("18:00:00","23:59:59"));
        }
        if (company1){
            companyList.add(1);
        }
        if (company2){
            companyList.add(2);
        }
        if (company3){
            companyList.add(3);
        }
        List<Aircraft> list=aircraftService.getAircraftList(date,area1,area2,timeList,companyList);
        if (list == null) {
            throw new MyException("444","查询失败");
        }
        for (Aircraft aircraft : list) {
            System.out.println(aircraft);
        }
        return new Result("200","查询成功",list);
    }

    @ApiOperation("查询当天的航空公司")
    @GetMapping("get-company")
    public Result getCompany(String date){
        List<String> list=aircraftService.getCompany(date);
        return new Result<>("200","查询成功",list);
    }

    @ApiOperation("查询剩余座位数量")
    @GetMapping("get-seat-left")
    public Result getSeatLeft(Integer id){
        if (aircraftService.getSeatLeft(id)<=0){
            throw new MyException("444","剩余座位不足");
        }
        return new Result("200","查询成功",aircraftService.getSeatLeft(id));
    }

    @ApiOperation("查询航班信息")
    @GetMapping("get-aircraft-by-id")
    public Result getAircraftByID(Integer id){
        Aircraft aircraft=aircraftService.getById(id);
        aircraft.setName(airlineService.getById(aircraft.getAirlineCompany()).getName());
        if (aircraft==null){
            throw new MyException("444","没有找到该航班信息");
        }
        return new Result<>("200","查询成功",aircraft);
    }
}
